﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using Useful.GUI;

namespace Useful
{
    public partial class ExComboBox : UserControl
    {
        [Browsable(true)]
        [DisplayName("Расширять выпадающий список")]
        [DefaultValue(true)]
        public bool ResizeDropDownToMaxString { get; set; }

        public event EventHandler SelectedValueChanged;

        public ExComboBox()
        {
            InitializeComponent();
            ResizeDropDownToMaxString = true;
        }

        public string Caption
        {
            get
            {
                return gbCaption.Text;
            }
            set
            {
                gbCaption.Text = value;
            }
        }

        public GroupBox GroupBoxControl
        {
            get
            {
                return gbCaption;
            }
        }

        public SearchedComboBox2 ComboBoxControl
        {
            get
            {
                return cmbValue;
            }
        }

        public int DropDownHeight
        {
            get
            {
                return cmbValue.DropDownHeight;
            }
            set
            {
                cmbValue.DropDownHeight = value;
            }
        }

        private void SetDropDownWidth()
        {
            if (ResizeDropDownToMaxString)
                cmbValue.DropDownWidth = GuiHelper.GetComboBoxMaxTextWith(cmbValue);
        }

        private void cmbValue_DropDown(object sender, EventArgs e)
        {
            SetDropDownWidth();
        }

        public object SelectedItem
        {
            get
            {
                return cmbValue.SelectedItem;
            }
            set
            {
                cmbValue.SelectedItem = value;
            }
        }

        public int SelectedIndex
        {
            get
            {
                return cmbValue.SelectedIndex;
            }
            set
            {
                cmbValue.SelectedIndex = value;
            }
        }

        public object SelectedValue
        {
            get
            {
                return cmbValue.SelectedValue;
            }
            set
            {
                cmbValue.SelectedValue = value;
            }
        }

        public int? SelectedIntValue
        {
            get
            {
                return GuiHelper.GetComboBoxValue(cmbValue);
            }
            set
            {
                cmbValue.SelectedValue = value ?? -1;
            }
        }

        public int? SelectedIntValue2
        {
            get
            {
                return GuiHelper.GetComboBoxValue2(cmbValue);
            }
            set
            {
                GuiHelper.SetComboBoxValue2(cmbValue, value);
            }
        }

        public override string Text
        {
            get
            {
                return cmbValue.Text;
            }
            set
            {
                cmbValue.Text = value;
            }
        }


        public void InitDefault(string display, string value, object dataSource)
        {
            GuiHelper.InitComboBox(cmbValue, display, value, dataSource);
        }

        public void InitDefault(string display, string value, BindingSource bs, object dataSource)
        {
            GuiHelper.InitComboBox(cmbValue, display, value, bs, dataSource);
        }

        public T GetDataSource<T>() where T : class
        {
            if (cmbValue.DataSource is BindingSource)
                return ((BindingSource)cmbValue.DataSource).DataSource as T;
            else
                return cmbValue.DataSource as T;
        }

        private void cmbValue_SelectedValueChanged(object sender, EventArgs e)
        {
            if (SelectedValueChanged != null)
                SelectedValueChanged(sender, EventArgs.Empty);
        }
    }
}
